package com.cjl.learn.builder.way3.clone;

import com.cjl.learn.builder.way3.entity.Sheep;
import com.cjl.learn.builder.way3.entity.Toy;
import sun.reflect.annotation.ExceptionProxy;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * 深拷贝2(推荐)
 *  序列化
 * Created by Enzo Cotter on 2019-08-23.
 */
public class CloneWay3 {

    public static void main(String[] args) throws Exception {
        Sheep sheep = new Sheep();
        Toy toy = new Toy();
        toy.setName("ball");
        sheep.setName("duoli");
        sheep.setToy(toy);

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);

        objectOutputStream.writeObject(sheep);

        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
        ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);

        Sheep clone = (Sheep) objectInputStream.readObject();

        System.out.println(sheep == clone); // false
        System.out.println(sheep.getToy() == clone.getToy()); // false
    }
}
